package com.bjc.principle.simplefactory.order;

import com.bjc.principle.simplefactory.pizza.GreekPizza;
import com.bjc.principle.simplefactory.pizza.Pizza;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * @program: DesignPattern
 * @description:
 * @author: bjc
 * @create: 2020-12-16 19:50
 */
public class OrderPizza {

//    public OrderPizza() {
//        Pizza pizza = null;
//        String orderTypes; //订购披萨的类型
//        do {
//            orderTypes = getType();
//            if (orderTypes.equals("greek")){
//                pizza = new GreekPizza();
//                pizza.setName("希腊披萨");
//            }else if (orderTypes.equals("cheese")){
//                pizza = new GreekPizza();
//                pizza.setName("奶酪披萨");
//            }else {
//                break;
//            }
//            //输出披萨制作过程
//            pizza.prepare();
//            pizza.bake();
//            pizza.cut();
//            pizza.box();
//        }while (true);
//    }

    //定义一个简单工厂对象
    SimpleFactory simpleFactory;
    Pizza pizza;

    public OrderPizza(SimpleFactory simpleFactory){
        setFactory(simpleFactory);
    }

    public void setFactory(SimpleFactory simpleFactory){
        String orderType = ""; //用户输入的

        this.simpleFactory = simpleFactory; //设置简单工厂对象
        do {
            orderType = getType();
            pizza = this.simpleFactory.createPizza(orderType);
            //输出pizza
            if (null!=pizza){
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            }else {
                System.out.println("订购披萨失败");
                break;
            }
        }while (true);
    }

    //写一个方法，可以获取用户希望订购的披萨种类
    private String getType(){
        try{
            BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("input pizza type");
            String s = strin.readLine();
            return s;
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }
    }
}
